`
https://leetcode.cn/problems/online-election/
`

/**
 * @param {number[]} persons
 * @param {number[]} times
 */
var TopVotedCandidate = function (persons, times) {
  const n = persons.length
  // 每个候选人的票数
  const tickets = new Array(n).fill(0)
  // 当前最大的票数
  let curMaxTicket = 0
  // 记录某时刻领先的候选人 [[time, person], ...]
  const record = []
  record.push([-1, -1]) // 哨兵，方便后续逻辑处理
  for (let i = 0; i < n; i++) {
    const person = persons[i], time = times[i]
    // 更新得票数据
    if (++tickets[person] >= curMaxTicket) {
      curMaxTicket = tickets[person]
      // 如果当前领先的仍然领先，则不用记录
      const last = record.at(-1)
      if (last) {
        const lastPerson = last[1]
        if (lastPerson !== person) {
          record.push([time, person])
        }
      }
    }
  }
  record.shift() // 去掉哨兵
  this.record = record
};

/** 
 * @param {number} t
 * @return {number}
 */
TopVotedCandidate.prototype.q = function (t) {
  // 找到最后一个 <= t 的 record
  const target = _.sortedIndexBy(this.record, [t + 1], ([time]) => time) - 1
  return this.record[target][1]
};

/** 
 * Your TopVotedCandidate object will be instantiated and called as such:
 * var obj = new TopVotedCandidate(persons, times)
 * var param_1 = obj.q(t)
 */